package com.graduate.work.graduatework.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.graduate.work.graduatework.contant.SourceContant;
import com.graduate.work.graduatework.entity.AirTicketDetail;
import com.graduate.work.graduatework.utils.DataHandleUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.*;
import java.util.*;

/**
 * @author: cuibingqi.
 * @date: 17/2/11 下午2:29.
 * @descripe: 解析通过抓包获取的数据
 */
@Service
public class ParseAirTicketDataService {

    @Autowired
    private DataHandleUtil dataHandleService;

    /**
     * 将"去哪儿"单程机票信息映射成AirTiketInfoEntity对象
     * @param DCity1
     * @param ACity1
     * @param DDate1
     * @return AirTiketInfoEntity对象
     */
    public List<AirTicketDetail> parseQuNAirTicket(String DCity1, String ACity1, String DDate1) {
        DDate1 = "2017-05-05";
        //        JSONObject dataAllJson = dataHandleService.captureOwAirTicketList(DCity1,ACity1,DDate1);
        List<AirTicketDetail> list = new ArrayList<AirTicketDetail>();

        JSONObject dataAllJson = file("/Users/cuibingqi/Documents/quow.txt");
        JSONObject oneway_data = dataAllJson.getJSONObject("oneway_data");
        //航班信息
        JSONObject flightInfo = oneway_data.getJSONObject("flightInfo");
        //价格信息
        JSONObject priceInfo = oneway_data.getJSONObject("priceInfo");

        List<String> keyList = new ArrayList<String>(flightInfo.keySet());
//        TreeSet<String> treeSet = new TreeSet<String>();
        for(int i = 0;i < 10 ;i++){
            AirTicketDetail airTiketDetailEntity = new AirTicketDetail();

            JSONObject flightInfoValue = flightInfo.getJSONObject(keyList.get(i));
            //出发时间
            String depTime = DDate1 + " " + flightInfoValue.getString("dt");
            //到达时间
            String arrTime = DDate1 + " " + flightInfoValue.getString("at");
            //出发机场
            String depAirport = flightInfoValue.getString("da");
            //到达机场
            String arrAirport = flightInfoValue.getString("aa");
            //航班号
            String flightNumber = flightInfoValue.getString("co");
            //出发城市
            JSONObject departureAirport = dataAllJson.getJSONObject("departureAirport");
            String depCity = departureAirport.getString("zh");
            //到达城市
            JSONObject arrivalAirport = dataAllJson.getJSONObject("arrivalAirport");
            String arrCity = arrivalAirport.getString("zh");
            //解析航线价格
            JSONObject priceInfoValue = priceInfo.getJSONObject(keyList.get(i));
            int price = priceInfoValue.getInteger("lowpr");

            airTiketDetailEntity.setFlightNumber(flightNumber);
            airTiketDetailEntity.setPrice(price);
            airTiketDetailEntity.setDepAirPort(depAirport);
            airTiketDetailEntity.setArrTime(arrTime);
            airTiketDetailEntity.setBuildPrice(50);
            airTiketDetailEntity.setArrAirPort(arrAirport);
            airTiketDetailEntity.setArrCity(arrCity);
            airTiketDetailEntity.setDepCity(depCity);
            airTiketDetailEntity.setDepTime(depTime);
            airTiketDetailEntity.setSource(SourceContant.QN);

            list.add(airTiketDetailEntity);
        }

        System.out.println(list);
        return list;
    }

    /**
     * 将"飞猪"单程机票信息映射成AirTiketInfoEntity对象
     * @param DCity1
     * @param ACity1
     * @param DDate1
     * @return
     */
    public List<AirTicketDetail> parseFeZAirTicket(String DCity1, String ACity1, String DDate1) {
//        JSONObject dataAllJson = dataHandleService.captureOwAirTicketList(DCity1,ACity1,DDate1);
        List<AirTicketDetail> list = new ArrayList<AirTicketDetail>();

        JSONObject dataAllJson = file("/Users/cuibingqi/Documents/fzow.txt");
        //解析航司二字吗集合
        JSONObject data = dataAllJson.getJSONObject("data");
        JSONObject aircodeNameMapJson = data.getJSONObject("aircodeNameMap");
        Map<String,String> aircodeNameMap = toMap(aircodeNameMapJson);
        //解析机场三字码集合
        JSONObject airportMapJson = data.getJSONObject("airportMap");
        Map<String,String> airportMap = toMap(airportMapJson);
        //解析出发城市
        String depCityName = data.getString("depCityName");
        //解析到达城市
        String arrCityName = data.getString("arrCityName");
        //解析航班组
        JSONArray flightArr = data.getJSONArray("flight");
        for(int i=0;i<10;i++){
            AirTicketDetail airTiketDetailEntity = new AirTicketDetail();

            JSONObject flight = flightArr.getJSONObject(i);
            //到达机场
            String arrAirport = airportMap.get(flight.getString("arrAirport"));
            //出发机场
            String depAirport = airportMap.get(flight.getString("depAirport"));
            //到达时间
            String arrTime = flight.getString("arrTime");
            //出发时间
            String depTime = flight.getString("depTime");
            //基建费用
            int buildPrice = flight.getInteger("buildPrice");
            //航班号
            String flightNo = flight.getString("flightNo");
            JSONObject cabin = flight.getJSONObject("cabin");
            int bestPrice = cabin.getInteger("bestPrice");

            airTiketDetailEntity.setArrAirPort(arrAirport);
            airTiketDetailEntity.setBuildPrice(buildPrice);
            airTiketDetailEntity.setDepAirPort(depAirport);
            airTiketDetailEntity.setFlightNumber(flightNo);
            airTiketDetailEntity.setArrCity(arrCityName);
            airTiketDetailEntity.setDepTime(depTime);
            airTiketDetailEntity.setDepCity(depCityName);
            airTiketDetailEntity.setArrTime(arrTime);
            airTiketDetailEntity.setPrice(bestPrice);
            airTiketDetailEntity.setSource(SourceContant.FZ);
            list.add(airTiketDetailEntity);
        }
        System.out.println(list.toString());
        return list;
    }
    /**
     * 将"美团"机票信息映射成AirTiketInfoEntity对象
     * @return
     */
    public List<AirTicketDetail> parseMTAirTicket(String DCity1, String ACity1, String DDate1) {
        DDate1 = "2017-05-04";

        //        JSONObject dataAllJson = dataHandleService.captureOwAirTicketList(DCity1,ACity1,DDate1);
        List<AirTicketDetail> list = new ArrayList<AirTicketDetail>();

        JSONObject dataAllJson = file("/Users/cuibingqi/Documents/mtow.txt");
        JSONArray data = dataAllJson.getJSONArray("data");
        for(int i = 0;i < 10;i++){
            JSONObject dataAtom = data.getJSONObject(i);
            JSONArray otas = dataAtom.getJSONArray("otas");
            //出发城市
            String depCity = dataAtom.getString("depart");
            //到达城市
            String arrCity = dataAtom.getString("arrive");
            //航班号
            String flightNumber = dataAtom.getString("fn");
            //到达时间
            String arrTime = DDate1 + " " + dataAtom.getString("a_time");
            //出发时间
            String depTime = DDate1 + " " + dataAtom.getString("s_time");
            //出发机场
            String depAirport = dataAtom.getString("departairport");
            //到达机场
            String arrAirport = dataAtom.getString("arriveairport");
            //价格
            int price = dataAtom.getInteger("price");
            int buildprice = 50;

            AirTicketDetail airTiketDetailEntity = new AirTicketDetail();
            airTiketDetailEntity.setArrAirPort(arrAirport);
            airTiketDetailEntity.setDepTime(depTime);
            airTiketDetailEntity.setDepCity(depCity);
            airTiketDetailEntity.setArrCity(arrCity);
            airTiketDetailEntity.setBuildPrice(buildprice);
            airTiketDetailEntity.setArrTime(arrTime);
            airTiketDetailEntity.setDepAirPort(depAirport);
            airTiketDetailEntity.setFlightNumber(flightNumber);
            airTiketDetailEntity.setPrice(price);
            airTiketDetailEntity.setSource(SourceContant.MT);

            list.add(airTiketDetailEntity);

        }
        System.out.println(list);
        return list;
    }

    public static Map toMap(JSONObject jsonObject) throws JSONException {


        Map result = new HashMap();
        Set<String> keySet = jsonObject.keySet();
        String key = null;
        String value = null;

        for (String tmpKey : keySet) {

            key = tmpKey;
            value = jsonObject.getString(key);
            result.put(key, value);

        }
        return result;

    }
    public static JSONObject file(String filename){
        File file = new File(filename);
        BufferedReader reader = null;
        String result = "";
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                // 显示行号
                tempString = StringUtils.trimAllWhitespace(tempString);
                result += tempString;
                line++;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        result = StringUtils.trimAllWhitespace(result);
        result = result.replace(");","");
        result = result.replace("jsonp189(","");
        result = result.replace("(","");
        result = result.replace(")","");
       return JSON.parseObject(result);
    }
}
